<div class="layuimini-container layuimini-page-anim" divname="deviceAccess">
    <div class="layuimini-main">



        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> <i class="layui-icon layui-icon-key"></i>授权</button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="search"> <i class="layui-icon"></i> 查询 </button>

                <button class="layui-btn layui-btn-sm" lay-event="refresh"> 刷新 </button>

                <button class="layui-btn layui-btn-sm" lay-event="reUpload"> 重新上传勾选项 </button>
                <button class="layui-btn layui-btn-sm" lay-event="reUploadAll"> 全部重新上传 </button>

                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="clear"> 清空所有 </button>
            </div>
        </script>

        <table class="layui-hide" id="deviceAccessPageTable" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<style>
    .label_upload {
        color: #0034FF;
    }

    .label_wait {
        color: #8C7F12;
    }

    .label_delete {
        color: #DC143C;
    }

    .label_error {
        color: #FF5722;
        font-weight: bold;
    }
</style>

<script>
    layui.use(['form', 'table', 'miniPage', 'element', 'faceAPI'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            miniPage = layui.miniPage;
        var faceAPI = layui.faceAPI;


        var UploadStatusTable = [];
        UploadStatusTable[0] = '<span class="label label_wait">未上传</span>';
        UploadStatusTable[1] = '<span class="label label_upload">已上传</span>';
        UploadStatusTable[2] = '<span class="label label_delete">待删除</span>';

        var UploadResultTable = [];
        UploadResultTable[1] = "人员指纹特征码重复";
        UploadResultTable[2] = "人员人脸特征码重复";
        UploadResultTable[3] = "人员卡号重复";
        UploadResultTable[4] = "存储空间已满";
        UploadResultTable[5] = "照片不可识别";
        UploadResultTable[6] = "特征码不可识别";
        UploadResultTable[7] = "未收到附加包";
        UploadResultTable[8] = "Json无法解析";
        //UploadResultTable[8] = "图像拷贝出错";
        //UploadResultTable[9] = "没有图像";
        //UploadResultTable[10] = "人员信息写入数据库失败";
        //UploadResultTable[11] = "图像画质过低";
        //UploadResultTable[12] = "人脸相似度太高";
        //UploadResultTable[13] = "超过一个注册限制";
        //UploadResultTable[14] = "图像格式不符合要求";
        //UploadResultTable[15] = "应用程序内部错误";
        //UploadResultTable[16] = "重复注册错误";
        //UploadResultTable[17] = "人脸被人为是别人";
        //UploadResultTable[18] = "人脸注册校验成功";
        //UploadResultTable[19] = "人脸注册校验失败";
        //UploadResultTable[20] = "Json数据解析出错";
        //UploadResultTable[21] = "管理员数量超出最大容量";

        IniSearchPar();


        table.render({
            elem: '#deviceAccessPageTable',
            //**************************异步请求开始***********************
            url: 'api/DeviceAccess/GetList',
            where: faceAPI.AccessSearchPageResult.field,
            method: 'post',
            headers: {
                token: 'sasasas',
                Authorization: "Bearer " + faceAPI.GetJWTToken()
            },//增加Token验证头
            request: {
                pageName: 'pageIndex', //页码的参数名称，默认：page
                limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function (res) { //res 即为原始返回的数据
                if (res.errCode == 0) {
                    var data = res.content;
                    var ret = {
                        "code": res.errCode, //解析接口状态
                        "msg": res.error, //解析提示文本
                        "count": data.totalCount, //解析数据长度
                        "data": data.dataList //解析数据列表
                    };
                    return ret;

                } else {
                    /*layer.open({
                        title: '请求错误',
                        content: '(' + res.errCode + ')' + res.error
                    });  */

                    return {
                        "code": 0, //解析接口状态
                        "msg": '', //解析提示文本
                        "count": 0, //解析数据长度
                        "data": [] //解析数据列表
                    };
                }

            },
            //**************************异步请求结束***********************

            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                { type: "checkbox", width: 50 },
                { field: 'deviceID', width: 160, title: 'SN', sort: true },
                { field: 'deviceName', width: 160, title: '门名称', sort: true },
                { field: 'employeeID', width: 120, title: '人员编号', sort: true },
                { field: 'employeeName', width: 140, title: '人员姓名', sort: true },
                {
                    field: 'access', width: 90, title: '凭证', sort: false,
                    templet: function (d) {
                      
                        var sBuf = [];
                        if (d.employeeIC > 0) {
                            sBuf.push('<i class="PeopleAccessKey fa fa-credit-card"  typeTitle="卡号"  type="text"  value = "' + d.employeeIC + '"></i>');
                        }
                        if (d.employeePhoto!=null&&d.employeePhoto.length > 0) {
                            sBuf.push('<i class="PeopleAccessKey fa fa-child" typeTitle="照片" type="image" value = "' + d.employeePhoto + '"></i>');
                        }
                        if (d.employeePassword !=null&&d.employeePassword.length > 0) {
                            sBuf.push('<i class="PeopleAccessKey fa fa-at" typeTitle="密码" type="text" value = "' + d.employeePassword + '"></i>');
                        }

                        return sBuf.join(' ');
                    }
                },
                {
                    field: 'employeeRoot', width: 90, title: '人员角色', sort: false,
                    templet: function (d) {
                        var sBuf = [];
                        if (d.employeeRoot == 0) {
                            return "用户";
                        }
                        else if (d.employeeRoot == 1) {
                            return "管理员";
                        }
                        else if (d.employeeRoot == 2) {
                            return "黑名单";
                        }
                        return "错误 " + d.employeeRoot;
                    }
                },
                {
                    field: 'timeGroupID', width: 120, title: '开门时段', sort: false,
                    templet: function (d) {
                        return "时段 " + d.timeGroupID;
                    }
                },
                {
                    field: 'devicePassNumber', width: 120, title: '开门次数', sort: false,
                    templet: function (d) {
                        if (d.devicePassNumber == 65535) return "无限制";
                        if (d.devicePassNumber == 0) return "已失效";
                        return d.devicePassNumber + ' 次';
                    }
                },
                { field: 'devicePassEnd', width: 180, title: '截止日期', sort: false },
                {
                    field: 'uploadStatus', width: 260, title: '上传状态', sort: false,
                    templet: function (d) {
                        var sBuf = [];
                        if (d.uploadStatus >= 0 && d.uploadStatus <= 2) {
                            if (d.uploadStatus == 1) {
                                if (d.uploadResult != 0) {
                                  //  debugger
                                    //上传完毕，但是有错误
                                    var sTip = '<div class="label_error showtoolTip"  uploadtime="' + d.uploadTime + '"><i class="fa fa-exclamation-triangle "></i>' + UploadResultTable[d.uploadResult] + '</div>';
                                    return sTip;
                                } else {
                                    var sTip = '<span class="label label_upload showtoolTip" uploadtime="' + d.uploadTime + '" >已上传</span>';
                                    return sTip;
                                }

                            } else {
                                return UploadStatusTable[d.uploadStatus];
                            }


                        }
                        return "错误 " + d.uploadStatus;
                    }
                },
                { title: '操作', Width: 150, toolbar: '#currentTableBar' }
            ]],
            limits: [100, 200, 300, 400, 500, 600],
            limit: 100,
            page: true,
            skin: 'line',
            done: function (res, curr, count) {
                //绑定事件
                if (res.count > 0) {
                    //lay-filter="PeoplePageTableFilter"
                    bindPeopleAccessKey();
                }
            }
        });
        function bindPeopleAccessKey() {
            $("[lay-id=deviceAccessPageTable] i.PeopleAccessKey").mouseenter(AccessKey_mouseenter);
            $("[lay-id=deviceAccessPageTable] i.PeopleAccessKey").mouseleave(() => {
                if (iMouseenter_TipIndex > 0)
                    layer.close(iMouseenter_TipIndex);
            });
            $(".showtoolTip").mouseenter(uploadTime_mouseenter);
            $(".showtoolTip").mouseleave(() => {
                if (iMouseenter_TipIndex > 0)
                    layer.close(iMouseenter_TipIndex);
            });
        }
        table.on('sort(deviceAccessPageTableFilter)', function (obj) {
            bindPeopleAccessKey();

        });

        // 人员凭证显示提示框
        var iMouseenter_TipIndex = 0;
        function AccessKey_mouseenter() {
            var that = this;
            if (iMouseenter_TipIndex > 0)
                layer.close(iMouseenter_TipIndex);
            var spn = $(that);
            var sType = spn.attr("type");
            var stypeTitle = spn.attr("typeTitle");
            var sValue = spn.attr("value");
            var sHtml = '';//'<div class="layui-card">';
            //sHtml = sHtml + '<div class="layui-card-header">' + stypeTitle + '</div>';
            //sHtml = sHtml + '<div class="layui-card-body">';



            var par = { area: ['240px', '160px'] };
            //debugger;
            if (sType == "text") {

                sHtml = '<h2>' + stypeTitle + '</h2>' + '<h3>' + sValue + '</h3>';
            }
            else {
                par.area[0] = '260px';
                par.area[1] = '380px';
                sHtml = '<h2>' + stypeTitle + '</h2>' + '<img id="showPeopleImg" src="' + sValue + '" width="240"/>';
            }


            //sHtml = sHtml + '</div></div>';

            //console.log(sHtml);
            iMouseenter_TipIndex = layer.tips(sHtml, that, par);
        }

        function uploadTime_mouseenter() {
            var that = this;
            if (iMouseenter_TipIndex > 0)
                layer.close(iMouseenter_TipIndex);
            var spn = $(that);
            var sUploadtime = spn.attr("uploadTime");

            var sHtml = '';//'<div class="layui-card">';


            var par = { area: ['260px', '30px'] };
            //debugger;

            sHtml = '<h3>上传时间：' + sUploadtime + '</h3>';

            iMouseenter_TipIndex = layer.tips(sHtml, that, par);
        }

        /**
         * toolbar事件监听
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {   // 监听添加操作
                var content = miniPage.getHrefContent('page/Access/add.html');
                var openWH = miniPage.getOpenWidthHeight();
                faceAPI.AccessAddPageResult = null;
                var index = layer.open({
                    title: '权限授权',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {
                        var rst = faceAPI.AccessAddPageResult;
                        if (rst != null && rst.result) {
                            //执行搜索重载
                            refreshTable(true);
                        };

                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if (obj.event === 'search') {  // 监听删除操作
                var content = miniPage.getHrefContent('page/Access/search.html');
                var openWH = miniPage.getOpenWidthHeight();

                var index = layer.open({
                    title: '查询权限',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {
                        var rst = faceAPI.AccessSearchPageResult;
                        if (rst != null && rst.result) {
                            refreshTable(false);
                        };
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
            else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('deviceAccessPageTable')
                    , data = checkStatus.data;
                var accessIDs = [];
                console.log(data);
                data.forEach(function (item, index) { accessIDs.push(item.accessID); });
                if (accessIDs.length == 0) {
                    layer.alert('请勾选需要删除的权限');
                    return;
                }
                faceAPI.Ajax({
                    type: 'Post',
                    data: { accessIDs },
                    url: '/api/DeviceAccess/Delete',
                    success: function (result) {
                        if (result.result == false) {
                            layer.alert(result.error, { icon: 2 });
                        }
                        else {
                            //刷新页面
                            refreshTable(false);
                        }

                    }
                });
            }
            else if (obj.event === 'refresh') {  // 监听删除操作
                refreshTable(true);
            }
            else if (obj.event === 'clear') {  // 监听删除操作
                layer.confirm('操作确认：清空所有人员权限？', function (index) {
                    faceAPI.Ajax({
                        type: 'Delete',
                        url: '/api/DeviceAccess/ClearAccess',
                        success: function (result) {
                            if (result.result == false) {
                                layer.alert(result.error, { icon: 2 });
                            }
                            else {
                                //刷新页面
                                refreshTable(true);
                            }

                        }
                    });
                    layer.close(index);
                });
            }
            else if (obj.event === 'reUpload') {
                btnReupload();
            }
            else if (obj.event === 'reUploadAll') {
                btnReuploadAll();
            };
        });

        function btnReupload() {//勾选项重新上传
            var checkStatus = table.checkStatus('deviceAccessPageTable')
                , data = checkStatus.data;
            var accessIDs = [];
            data.forEach(function (item, index) { accessIDs.push(item.accessID); });
            if (accessIDs.length == 0) {
                layer.alert('请勾选需要重新上传的权限');
                return;
            }
            layer.confirm('操作确认：将勾选的权限重新上传？<br/>说明：已删除的权限不会重新上传！', function (index) {
                faceAPI.Ajax({
                    type: 'Post',
                    data: { accessIDs:accessIDs},
                    url: '/api/DeviceAccess/Reupload',
                    success: function (result) {
                        if (result.result == false) {
                            layer.alert(result.error, { icon: 2 });
                        }
                        else {
                            //刷新页面
                            refreshTable(true);
                        }

                    }
                });
                layer.close(index);
            });
        }

        function btnReuploadAll() {//所有权限重新上传
            layer.confirm('操作确认：将所有权限全部重新上传？<br/>说明：已删除的权限不会重新上传！', function (index) {
                faceAPI.Ajax({
                    type: 'Post',
                    url: '/api/DeviceAccess/ReuploadAll',
                    success: function (result) {
                        if (result.result == false) {
                            layer.alert(result.error, { icon: 2 });
                        }
                        else {
                            //刷新页面
                            refreshTable(true);
                        }

                    }
                });
                layer.close(index);
            });
        }


        function refreshTable(bIni) {
            if (bIni) IniSearchPar();
            var rst = faceAPI.AccessSearchPageResult;
            table.reload('deviceAccessPageTable', {
                page: {
                    curr: 1
                }, where: rst.field
            }, 'data');
        }
        function IniSearchPar() {
            var par = {
                field: {
                    EmployeeRoot: 255,
                    UploadStatus: 255,
                    UploadResult: 255
                },
                result: true
            };

            faceAPI.AccessSearchPageResult = par;
        }



        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;

            if (obj.event === 'edit') {

                var content = miniPage.getHrefContent('page/Access/edit.html');
                var openWH = miniPage.getOpenWidthHeight();
                faceAPI.AccessEditPage = data;//向layer页面传值，传值主要代码
                faceAPI.AccessEditPageResult = null;
                var index = layer.open({
                    title: '编辑人员权限',
                    type: 1,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: [openWH[0] + 'px', openWH[1] + 'px'],
                    offset: [openWH[2] + 'px', openWH[3] + 'px'],
                    content: content,
                    end: function (layero, index) {

                        var rst = faceAPI.AccessEditPageResult;
                        console.log(rst);
                        if (rst != null && rst.result) {
                            var newAccess = rst.content;
                            //执行搜索重载
                            obj.update(newAccess);
                        };

                    }
                });
                // layer.full(index);
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            }
            else if (obj.event === 'delete') {

                layer.confirm('操作确认：<br/>删除<br/>人员：[' + data.employeeName + '(' + data.employeeID + ')]<br/> 在<br/> 门：[' + data.deviceName + ']<br/>上的权限吗?', function (index) {
                    var accessIDs = [];
                    accessIDs.push(data.accessID);
                    faceAPI.Ajax({
                        type: 'Post',
                        data: { accessIDs },
                        url: '/api/DeviceAccess/Delete',
                        success: function (result) {
                            if (result.result == false) {
                                layer.alert(result.error, { icon: 2 });
                            }
                            else {
                                //刷新页面
                                refreshTable(false);
                            }

                        }
                    });


                    layer.close(index);
                });
            }
        });
    });



</script>